package org.hagopa.games.moneypool.msgs.ContentTypes.game;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hagopa.games.moneypool.game.gameobjects.PropertyStatusEnum;
import org.hagopa.games.moneypool.msgs.CommandsEnum;
import org.hagopa.games.moneypool.msgs.IMsgContent;


public class UpdateStatus implements IMsgContent{
	
	private int marshalledSize = 0;
        private int playersRecords = 0;
	private List<Ustatus> updateListForPlayers;
        private int abId = 0; //absolute Id - independent from Board number
	private int upgradeLevel = 0; // TODO change to Enum
	private int currentCost = 0;
	private int fee = 0;
	private PropertyStatusEnum status = null;
	private int owner = 0; // player id in current Game
	
	public UpdateStatus()
        {
            updateListForPlayers = new ArrayList<Ustatus>();
        }
        
	public int getSize() {
                
		return marshalledSize;
	}

	public boolean addRecord(Ustatus newrec)
        {
            if(null == newrec)
                return false;
            this.updateListForPlayers.add(newrec);
            playersRecords++;
            return true;
        }
	public byte[] marshallToByte() {
		System.out.println("Marshall Player Location");
		ByteBuffer dataArray = ByteBuffer.allocate(1024);
		byte [] bytes = new byte[1024];
		dataArray.putInt(CommandsEnum.UPD_PLAYER.idx());
		//dataArray.putInt(UpdateTypeEnum.PLAYER_UPDATE.idx());
		
		dataArray.putInt(this.abId);
		dataArray.putInt(this.upgradeLevel);
		dataArray.putInt(this.currentCost);
		dataArray.putInt(this.fee);
                if(this.status != null)
                {
                    System.out.println("[UpdateStatus] (marshallToByte) Status:" + this.status.name());
                    dataArray.putInt(this.status.idx());
                }
                else
                {
                    System.out.println("[UpdateStatus] (marshallToByte) No Status for propid:" + this.abId +"assigned!!!!!");
                }
		dataArray.putInt(this.owner);

                dataArray.putInt(this.playersRecords);
		Iterator<Ustatus> itr = this.updateListForPlayers.iterator();
		int tot_sz = 0;

                byte [] tempbyte = new byte[512];
                
		while(itr.hasNext())
		{
			Ustatus tmp = (Ustatus)itr.next();
			//dataArray.put(tmp.marshallToByte());
                        System.arraycopy(tmp.marshallToByte(), 0, tempbyte, tot_sz, tmp.getTotal_size());
                        
			tot_sz+= tmp.getTotal_size();
                        System.out.println("[UpdateStatus] (marshallToByte) current total size:" + tot_sz);
		}
		dataArray.put(tempbyte);
		dataArray.rewind();
		this.marshalledSize = 8*4 + tot_sz ;
		dataArray.get(bytes,0,this.marshalledSize);
		System.out.println("[UpdateStatus] (marshallToByte) return " + this.marshalledSize + " size");
		return bytes;
		
	}

	
	public boolean unmarshallFromByte(byte[] content) {
		System.out.println("Marshall Update Status");
                ByteBuffer dataArray = ByteBuffer.wrap(content);
		if(null == dataArray)
		{
			return false;
		}
		dataArray.clear();
		this.abId = dataArray.getInt();
		this.upgradeLevel = dataArray.getInt();
		this.currentCost = dataArray.getInt();
		this.fee = dataArray.getInt();
		this.status = PropertyStatusEnum.getEnum(dataArray.getInt());
		this.owner = dataArray.getInt();
		return true;
	}
        public int getAbId() {
		return abId;
	}

	public void setAbId(int abId) {
		this.abId = abId;
	}

	public int getUpgradeLevel() {
		return upgradeLevel;
	}

	public void setUpgradeLevel(int upgradeLevel) {
		this.upgradeLevel = upgradeLevel;
	}

	public int getCurrentCost() {
		return currentCost;
	}

	public void setCurrentCost(int currentCost) {
		this.currentCost = currentCost;
	}

	public int getFee() {
		return fee;
	}

	public void setFee(int fee) {
		this.fee = fee;
	}

	public PropertyStatusEnum getStatus() {
		return status;
	}

	public void setStatus(PropertyStatusEnum status) {
		this.status = status;
	}

	public int getOwner() {
		return owner;
	}

	public void setOwner(int owner) {
		this.owner = owner;
	}

    public String getConcatContent() {
        String outstr = null;
        outstr = String.valueOf(CommandsEnum.UPD_PLAYER.idx()) + String.valueOf(this.abId)+ String.valueOf(this.upgradeLevel)
                 + String.valueOf(this.currentCost) + String.valueOf(this.fee) + String.valueOf(this.status.idx()) + String.valueOf(this.owner);
        Iterator<Ustatus> itr = this.updateListForPlayers.iterator();
        while(itr.hasNext())
        {
                Ustatus tmp = (Ustatus)itr.next();
                outstr += tmp.getConcatContent();

        }
        return outstr;
    }

}
